
package org.jma.app.ewisdom.entities;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Vector;

import org.jma.lib.soap.utils.XMLSerializable;

import com.thoughtworks.xstream.XStream;
/**
 * FileDescriptor class maintains the File data, Nowadays only stores:<br>
 * <ul>
 * <li>filename. file physical name.
 * <li>title. Title showed on Index.
 * <li>parent. Reference to its Content parent object
 * <li>mainNode. FileLocator object to get the Host location where the file was created.
 * <li>path. Complete path in the Index, content1/content2/file title
 * <li>stringHashCode. Hash code that complains the complete path
 * <li>owner. User creator
 * </ul>
 * May be in newer revisions it will store much more information about the File like creationTime
 * modificationTime...
 * <br>
 * @author Jesus Mari, jesmari@ono.com
 * @version 1.0
 * @since 1.0
 */
public class FileDescriptor extends EntityWithACL implements Cloneable, Serializable,XMLSerializable {

	HashMap versions = new HashMap();
	
	int ver=0;
	private String fileName;
	private String title;
	private String parent;
	private FileLocator mainNode;
	protected String path;
	private String stringHashCode;
	private String owner;

	
	/**
	 * returns a main node, main node is the node where the fileDescriptor was created
	 * @return the mainNode
	 */
	public FileLocator getMainNode() {
		return mainNode;
	}

	/**
	 * @param mainNode the mainNode to set
	 */
	public void setMainNode(FileLocator mainNode) {
		this.mainNode = mainNode;
	}

	/**
	 * Adds a new version of the File, the new version must be a {@link FilePart} array
	 * @param fileParts The FilePart array
	 */
	public void addNewVersion(FilePart[] fileParts){
		String versionName= "ver_" +ver;
		versions.put(versionName, fileParts);
		ver++;
	}
	
	/**
	 * Sets the next version number
	 * @param ver next version will be named as ver_x on x the version number
	 */
	public void setVer(int ver) {
		this.ver=ver;
	}
	
	/**
	 * @return the next version number
	 */
	public int getVer() {
		return ver;
	}
	
	/**
	 * remove a version from the version list
	 * @param versionName name must be ver_x, on x the version number
	 */
	public void removeVersion(String versionName){
		versions.remove(versionName);
	}
	
	/**
	 * Empty the versions HashMap
	 */
	public void removeAll(){
		versions= new HashMap();
		ver=0;
		//the old versions hashMap will be recollected...
	}
	
	/**
	 * returns the {@link FilePart} array of a given version.
	 * @param versionName name must be ver_x, on x the version number
	 * @return the FilePart array of a given version
	 */
	public FilePart[] getFileParts(String versionName){
		return (FilePart[]) versions.get(versionName);
	}
	
	/**
	 * Returns the String version names array
	 * @return
	 */
	public String[] getVersionsArray(){
		return (String[]) versions.keySet().toArray();
	}
	
	/**
	 * Returns the file name
	 * @return file name
	 */
	public String getFileName(){
		return fileName;
	}
	
	/**
	 * Assign the name of the file
	 * @param fileName name of the file
	 */
	public void setFileName(String fileName){
		this.fileName=fileName;
	}
	
	/**
	 * Title that will appear in the index
	 * @param title
	 */
	public void setTitle(String title){
		this.title=title;
	}
	
	/**
	 * Returns the title
	 * @return title
	 */
	public String getTitle(){
		return title;
	}
	
	/**
	 * Sets the parent CID
	 * @param parent CID
	 */
	public void setParent(String parent){
		this.parent=parent;
	}
	
	/**
	 * Returns the parent CID
	 * @return CID
	 */
	public String getparent(){
		return parent;
	}
	
	/**
	 * Sets the owner user name
	 * @param owner
	 */
	public void setOwner(String owner){
		this.owner=owner;
	}
	
	/**
	 * Returns the owner user name
	 * @return
	 */
	public String getOwner(){
		return owner;
	}
	
	/**
	 * @see org.jma.lib.soap.utils.XMLSerializable
	 */
    public String serializeToXML() {
    	XStream xstream = new XStream();
    	return xstream.toXML(this);
    }
    
	/**
	 * calculates its HashCode  
	 * @return Hashcode
	 */
	public int hashCode() {
		int result=0;
		result = (path != null ? path.hashCode() : 0);
        stringHashCode=""+result;    
        return result;
	}
	
	/**
	 * Returns the complete Path inside the index
	 * @return a string with the path
	 */
	public String getPath(){
		if (this.path==null) return ""; else return this.path;
	}
	
	/**
	 * Only for xml serialization purpose
	 * @param path
	 */
	public void setPath(String path){
		this.path=path;
	}
	
	/**
	 * HashCode in string form
	 * @return Hashcode
	 */
	public String getStringHashCode(){
		return stringHashCode;
	}
	
	/**
	 * Only for xml serialization purpose 
	 * @param stringHashCode
	 */
	public void setStringHashCode(String stringHashCode){
		this.stringHashCode=stringHashCode;
	}
	
	/**
	 * Calculates the hash code for a given path string
	 * @param path
	 * @return
	 */
	public static String calculateHashCode(String path){
	     String result;
	     result = ""+(path != null ? path.hashCode() : 0);
	     return result;
	}
	
	/**
	 * Overrides the equals method from Object class. Two FileDescriptors will be equals if
	 * their paths are equal.
	 */
	public boolean equals(Object content){		
		if (this.path==null && ((Content)content).getPath()==null) return true;
		else return (this.path.equals(((Content)content).getPath()));		
	}
}
